/**
 * 
 */
package edu.sjtu.seda.experiment;

import edu.sjtu.seda.log.LogService;
import edu.sjtu.seda.test.Message;
import edu.sjtu.seda.test.PrintMessage;
import edu.sjtu.seda.test.TransferMessage;
import edu.sjtu.seda.test.WriteToFile;

/**
 * @author john.zhu
 *
 */
public class EventDrivenTest {
	
	protected static LogService log = new LogService(EventDrivenTest.class);
	
	static class TestThread1 implements Runnable {

		private int i = 0;
		
        private EventDriven ed;
        
		
		public TestThread1(int i, EventDriven ed){
			this.i = i;
			this.ed = ed;
		}
		
		@Override
		public void run() {
			Message m = new Message("Message "+i);
			log.info("event "+m.getUID()+" will start");
			ed.pushEvent(new EventDrivenRunner(m,new PrintMessage(),new WriteToFile()));
		}
	}
	
	static class TestThread2 implements Runnable {

		private int i = 0;

		private EventDriven ed;

		public TestThread2(int i, EventDriven ed) {
			this.i = i;
			this.ed = ed;
		}

		@Override
		public void run() {
			Message m = new Message("Message "+i);
			log.info("event "+m.getUID()+" will start");
			ed.pushEvent(new EventDrivenRunner(m,new TransferMessage(),new WriteToFile()));
		}
	}
	
	public static void main(String[]args){
		long startNano = System.nanoTime();
		EventDriven ed = new EventDriven();
		Thread thread = new Thread(ed);
		thread.start();
		for (int i = 0; i < 200; i++) {
			if(i%2==0){
				Thread t1 = new Thread(new TestThread1(i, ed));
				t1.start();
			}
			else{
				Thread t2 = new Thread(new TestThread2(i, ed));
				t2.start();
			}
		}
	}

}
